home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: usinternet.com!not-for-mail
- From: Scott Jibben <sjibben@usinternet.com>
- Subject: Re: Pointers to member functions HOW?
- Message-ID: <31113B7A.4C82@usinternet.com>
- Date: Thu, 01 Feb 1996 16:15:22 -0600
- Organization: Jibben Software
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
- MIME-Version: 1.0
- References: <31067074.6B53@compuserve.com> <4e9nh8$iji@news2.ios.com>
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
-
- Vlastimil Adamovsky wrote:
- >
- > Roberto Ortiz <74011.3205@compuserve.com> wrote:
- >
- > >I've been attempting for some time now to declare a pointer to a class'
- > >memeber function. I have no problem with pointers to regular functions, but
- > >with class members, I run into one of two problems:
- >
- > >a) I can't assign the function to the pointer.
- > >b) I can't call the pointer as a function.
- >
- > >whenever I solve one, I get the other.
- >
- > >Here's some example code in Borland C++:
- >
- > >#include <stdio.h>
- >
- > >void RegularFunction(int iVal)
- > >{
- > > printf("[%d]\n", iVal);
- > >};
- >
- > >class TTest {
- > >public:
- > > void MemberFunction(int iVal)
- > > {
- > > printf("[%d]\n", iVal);
- > > };
- > >};
- >
- > >void main()
- > >{
- > > typedef void (TFuncVoidInt)(int);
- >
- > > TFuncVoidInt *Func0;
- > > TFuncVoidInt *Func1;
- >
- > > void (TTest::*Func2)(int);
- >
- > > Func0 = RegularFunction;
- > > Func1 = TTest::MemberFunction;
- > > Func2 = &TTest::MemberFunction;
- >
- > > Func0(100);
- > > Func1(100);
- > > Func2(100);
- > >};
- >
- > >with this code, I get the following compiler messages:
- >
- > >Compiling PTEST.CPP:
- > >Error PTEST.CPP 26: Cannot convert 'void (TTest::*)(int)' to 'void (*)(int)'
- > >in function main()
- > >Error PTEST.CPP 31: Call of nonfunction in function main()
- > >function main()
- >
- > The compiling error messages are very correct:
- >
- > 1) you cannot cast member function to non-member. It is nonsense
-
- You can if the method is declared as static:
-
- class TTest {
- public:
- static void MemberFunction(int iVal)
- {
- printf("[%d]\n", iVal);
- };
- };
-
- This only allows for one instance of the method for all
- implementations of the class, much like a static member.
-
- --
- Scott Jibben, Jibben Software
- Galactic Overlord & Mines of Gorr BBS Door Games
- -----------------------------
- [EMAIL] sjibben@usinternet.com
- [WWW] http://www.usinternet.com/jsw
- [FTP] ftp.europa.com /outgoing/DOORS/jibben
- [FTP] ftp.cts.com /pub/dferber/jibben
- [FIDO] 1:282/115 [BBS] 612-379-8272 (10 USR 28.8
- Couriers)
- [VOICE] 612-757-5626 [FAX] 612-757-8687
-
-